oo::class create Keyword {
    superclass Plugin
    variable RenamedPadWidget Language KeyWordGroups Tags

    constructor {renamedTextWidget language} {
        set RenamedPadWidget $renamedTextWidget
        set Language $language
        array set Tags {}
        
        my initTags
    }
    
    method initTags {} {
        set cls [info object class $Language]
        foreach m [info class methods $cls] {
            if [regexp {getKeyWordsGroup(\d)} $m _match id] {
                foreach w [$Language $m] {
                    set Tags($w) K$id
                }
            }
        }
    }
    
    method getTag {w} {
        return [lindex [array get Tags $w] 1]
    }
    
    method hilightKeyword {from to} {
        set from [$Language getWordHeadIndex $RenamedPadWidget $from]
        set to [$Language getWordEndIndex $RenamedPadWidget $to]

        foreach t [$RenamedPadWidget tag names] {
            if [regexp {^K\d$} $t] {
                $RenamedPadWidget tag remove $t $from $to
            }
        }

        set tagList {}
        set content [$RenamedPadWidget get $from $to]
        foreach ids [regexp -all -inline -indices {[\w:.]+} $content] {
            set w [string range $content {*}$ids]
            if {[set tag [my getTag $w]] != {}} {;# $Language getKeyWordTag
                lappend tagList $ids $tag
            }
        }
        
        foreach {ids tag} $tagList {
            foreach {h t} $ids {
                set h "$from +${h}c"
                set t "$from +${t}c +1c"
            }
            $RenamedPadWidget tag add $tag $h $t
        }
    }

    method hilightKeywordAfterInsert {from content args} {
        my hilightKeyword $from [$RenamedPadWidget index "$from +[string length $content]c"]
    }

    method hilightKeywordAfterDelete {index args} {
        my hilightKeyword $index $index
    }

    method hilightKeywordAfterReplace {from to content args} {
        my hilightKeyword $from [$RenamedPadWidget index "$from +[string length $content]c"]
    }

    method getPluginEvents {} {
        return {
            {afterInsert hilightKeywordAfterInsert}
            {afterDelete hilightKeywordAfterDelete}
            {afterReplace hilightKeywordAfterReplace}
        }
    }
}